home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.08 Aug 89 / POOPDraw Code ƒ / LIBR Misc Fns.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-06  |  7.6 KB  |  314 lines  |  [TEXT/KAHL]

  1. /********************************************************************/
  2. /*                                                                    */
  3. /*                        THE POOP SHELL                                */
  4. /*                                                                    */
  5. /********************************************************************/
  6. /*        An Adventure in Pseudo-Object-Oriented-Programming            */
  7. /********************************************************************/
  8. /*
  9. *   >>>    File name:        Library
  10. *
  11. *      >>>    Purpose:        A bunch of routines which don't fit elsewhere
  12. *     >>>    Project:        POOPDraw        
  13. *     >>>    Date:            6/8/89
  14. *      >>>    By:                Adam Treister
  15. *
  16. */
  17. /********************************************************************/
  18. /*    For Your Information            1802 Hillside Rd. SB CA 93101    */
  19. /********************************************************************/
  20. #include "PoopDrawInc"
  21.  
  22.  
  23. #define TYPE    'DWNG'            /* data file type id */
  24. #define CREATOR 'POOP'
  25. #define FILETYPE 'DWNG'
  26. static Point where = {90,90};
  27. SFTypeList myTypes = { TYPE, '\0', '\0', '\0'};
  28.  
  29. /********************************************************************/
  30. /*------------------------------------------------------------------*/
  31.  
  32. void InitMacintosh()
  33. {    
  34.     MaxApplZone();
  35.     MoreMasters();    MoreMasters();
  36.     MoreMasters();    MoreMasters();
  37.     MoreMasters();    MoreMasters();
  38.     InitGraf(&thePort);                /*    Initialize the Macintosh Toolbox Manager    */
  39.     InitFonts();
  40.     FlushEvents(everyEvent, 0);
  41.     InitWindows();
  42.     InitMenus();
  43.     TEInit();
  44.     InitDialogs(NULL);                /* No Resume Procedure */
  45.     InitCursor();
  46. }
  47.  
  48. /*---------------------------------------- TESetString */
  49. void TESetString(teH,str)
  50. TEHandle teH;
  51. Str255 str;
  52. {
  53.     TESetText(&str[1],(long) str[0], teH);
  54. }
  55. /*---------------------------------------- TEGetString */
  56. void TEGetString(teH,str)
  57. TEHandle teH;
  58. Str255 str;
  59. {
  60.     register int i,len;
  61.     register char *hText;
  62.     
  63.     str[0] = len = MIN(255,(*teH)->teLength);
  64.     hText = *((*teH)->hText);
  65.     for (i = 1; i <= len; i++)        str[i] = hText[i-1];
  66. }
  67.  
  68. /*------------------------------------------------ DrawLine -------*/
  69.  
  70. void DrawLine(a,b)
  71. Point a,b;
  72. {    MoveTo(a.h,a.v);
  73.     LineTo(b.h,b.v);    
  74. }
  75. /*---------------------------------------- DrawPic */
  76.  
  77. void DrawPic(x,y,picture)
  78. int    x,y;
  79. PicHandle picture;
  80. {    Rect    destRect;
  81.     
  82.     SetRect(&destRect,x,y,
  83.             x + Width((**picture).picFrame),
  84.             y + Height((**picture).picFrame));
  85.     DrawPicture(picture,&destRect);
  86. }
  87. /*----------------------------------------------------- Width */
  88.  
  89. Width(r)
  90. Rect r;
  91. {    return(r.right - r.left);    }
  92.  
  93. /*------------------------------------------------------ Height */
  94. Height(r)
  95. Rect r;
  96. {    return(r.bottom - r.top);    }
  97.  
  98. /*------------------------------------------------------- SortRect -*/
  99.  
  100. void SortRect(rP)
  101. Rect *rP;
  102. {
  103.     int temp;
  104.     
  105.     if (rP->left > rP->right)    {temp = rP->left;  rP->left = rP->right; rP->right = temp;}
  106.     if (rP->top > rP->bottom)    {temp = rP->top;  rP->top = rP->bottom; rP->bottom = temp;}
  107.     
  108. }
  109. /*------------------------------------------------------------------*/
  110.  
  111. void TurnWatchOn()
  112. {    SetCursor(*(GetCursor(4)));
  113. }
  114. /*------------------------------------------------------------------*/
  115.  
  116. void TurnArrowOn()
  117. {    SetCursor(&arrow);
  118. }
  119.  
  120. /*---------------------------------------- GetDragRect */
  121. void GetDragRect(rp)
  122. Rect *rp;
  123. {    *rp = screenBits.bounds;    }
  124. /*------------------------------------------------------ OurWindow */
  125.  
  126. Boolean OurWindow(wP)
  127. WindowPeek    wP;
  128. {
  129.    if (!wP) return(FALSE);
  130.    return (wP->windowKind > 0);
  131. }
  132.  
  133. /* ------------------------------------------------------------ */
  134. /*                                                                */
  135. /*                    GET NEW HANDLE                                */
  136. /*                                                                */
  137. /* ------------------------------------------------------------ */
  138.  
  139. Handle GetNewHandle(hsize)
  140. long        hsize;    
  141. {
  142.         Handle    h;        
  143.             
  144.     h = NewHandle(hsize);        MEM_CHECK    
  145.     return(h);
  146. }
  147.  
  148. /* ------------------------------------------------------------ */
  149. /*                                                                */
  150. /*                    DisposeHandle                                */
  151. /*                                                                */
  152. /* ------------------------------------------------------------ */
  153.  
  154. void DisposeHandle(h)
  155. Handle        h;        
  156.     
  157. {                    
  158.     if (!h)                return;
  159.     DisposHandle(h);    MEM_CHECK    
  160. }
  161. /*----------------------------------------------------------------
  162. *
  163. *                        NullOutHandle(h)
  164. *
  165. *    May 25, 1988        created                        AST
  166. *
  167. *     -- never has been thoroughly tested!!!!
  168. *    -- there is also a better way to do it, by telling the memory
  169. *            manager to clear the memory when it allocates it
  170. *----------------------------------------------------------------*/
  171. void NullOutHandle(h)
  172. Handle h;
  173. {
  174.     NullOutBytes(*h,GetHandleSize(h));
  175. }    
  176. /*----------------------------------------------------------------
  177. *
  178. *                        NullOutBytes(p,siz)
  179. *
  180. *    May 25, 1988        created                        AST
  181. *    March 8,1989        spawned from NullOutHandle  AST
  182. *----------------------------------------------------------------*/
  183. void NullOutBytes(p,siz)
  184. Ptr   p;
  185. long    siz;
  186. {
  187.     while (siz--)        *p++ = (Byte) 0;
  188. }    
  189. /* ------------------------------------------------- Oops ----------- */
  190.  
  191.  
  192. void Oops(s, num, Recoverable)
  193. char        *s;                
  194. int            num;                    
  195. Boolean        Recoverable;    
  196.     
  197. {
  198.             Str255    numStr,temp;
  199.  
  200.       NumToString((long) num, &numStr);
  201.     *temp=0;
  202.     PStrCat (4,temp, s,"\p  ", numStr);    
  203.     
  204.     DebugStr(temp);    
  205.     if (!Recoverable)        ExitToShell(); 
  206. }
  207. /*---------------------------------------------------------PStrCat---*/
  208.  
  209.  
  210. PStrCat(count, dst)         
  211. register int    count;    /* # of strings (including dst) */
  212. unsigned char    *dst;    /* destination pascal string */
  213. {    
  214.     register    unsigned char    *dstPtr, *srcPtr;
  215.     register     unsigned char    **argList = &dst;
  216.     register    int                 argLen, totLen;
  217.     
  218.     if ((totLen = *dst) < 255) {
  219.         if (count > 30)
  220.             count = 30;            /* max. # of string args is 30 */
  221.         dstPtr = dst + totLen;    /* dstPtr = 1 past end of dst */
  222.         while (--count > 0 && totLen < 255) {
  223.             srcPtr = *++argList;    
  224.             argLen = srcPtr[0];
  225.             if (totLen + argLen > 255)
  226.                 argLen = 255 - totLen;    /* max totLen = 255 */
  227.             totLen += argLen;                
  228.             while (--argLen >= 0)        /* add arg's char to dst */
  229.                 *++dstPtr = *++srcPtr;        
  230.         }
  231.         dst[0] = totLen;                    /* sets length of dst */
  232.     }
  233. }
  234.  
  235. /*-------------------------------------------------------StrCopy----*/
  236. StrCopy(a,b)
  237. Str255 a,b;
  238.  
  239. {    int len = *b = *a;
  240.  
  241.     while (len-- >= 0)        *b++ = *a++;
  242. }
  243.  
  244. /*------------------------------------------------------------------*/
  245.  
  246. void WriteHandleToFile(h,replyP)
  247. Handle h;
  248. SFReply         *replyP;
  249. {
  250.                 long             nBytes;
  251.                 int                volSave,dataFork;
  252.                 Str255            s;
  253.                          
  254.     TurnWatchOn();
  255.     GetVol(&s,&volSave);
  256.     SetVol(NULL,replyP->vRefNum);
  257.     
  258.     FSDelete(replyP->fName,replyP->vRefNum);    /* Trash old file version */
  259.  
  260.     FILE_CHECK(Create(replyP->fName,replyP->vRefNum,CREATOR,FILETYPE));
  261.     FILE_CHECK(FSOpen(replyP->fName,replyP->vRefNum,&dataFork));
  262.     nBytes = GetHandleSize(h);
  263.     FILE_CHECK(FSWrite(dataFork,&nBytes,*h));
  264.     if     (nBytes != GetHandleSize(h))
  265.         Oops("\pnBytes Error in save",nBytes,FALSE);
  266.     
  267.     FILE_CHECK(FSClose(dataFork));
  268.  
  269.     SetVol(NULL,volSave);
  270.     TurnArrowOn();
  271. }
  272. /*------------------------------------------------------------------*/
  273. Handle ReadFileToHandle()
  274. {    
  275.             SFReply         reply;        
  276.             long             fSize;
  277.             int                dataFork;
  278.             Handle            h;
  279.             
  280.     SFGetFile( where, "\pOpen the document:", 0L, 1, myTypes, 0L, &reply );
  281.     if (reply.good)
  282.     {
  283.         FILE_CHECK(FSOpen(reply.fName,reply.vRefNum,&dataFork));
  284.         GetEOF(dataFork,&fSize);
  285.         h = GetNewHandle(fSize);
  286.         FILE_CHECK(FSRead(dataFork,&fSize,*h));
  287.         FILE_CHECK(FSClose(dataFork));
  288.         return(h);
  289.     }
  290.     else return(NULL);
  291. }
  292. /*------------------------------------------------------------------*/
  293. Rect GetWindowRect(wP)        /* there MUST be a more direct way to do this !! */
  294. WindowPtr wP;
  295. {
  296.     Point topleft;
  297.     Rect wRect;
  298.     
  299.     SetPt(&topleft,0,0);
  300.     LocalToGlobal(&topleft);
  301.     SetRect(&wRect,topleft.h,topleft.v,
  302.         topleft.h+Width(wP->portRect),topleft.v + Height(wP->portRect));
  303.     return(wRect);
  304. }
  305. /*------------------------------------------------------------------*/
  306. WindowPtr MyFrontWindow()
  307. {
  308.     WindowPeek wP;
  309.     
  310.     wP = FrontWindow();
  311.     while (wP AND wP->windowKind < 0)
  312.         wP = wP->nextWindow;
  313.     return(wP);
  314. }